﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.XuLyBaoHanhSuaChua
{
    public partial class frmNhanVeTuNCC : Form
    {
        private int _phien_ThietBiBH;
        private string _maThietBiBH;
        private frmDsXuLyBHSC _callerForm;


        public frmNhanVeTuNCC(int inPhien_ThietBiBH, string inMaThietBiBH, string inTenNCC, frmDsXuLyBHSC inCallerForm)
        {
            InitializeComponent();
            cboNhaCungCap.Items.Insert(0, inTenNCC);
            cboNhaCungCap.SelectedIndex = 0;
            _phien_ThietBiBH = inPhien_ThietBiBH;
            _maThietBiBH = inMaThietBiBH;
            groupBox1.Text = "Thiết bị: " + _maThietBiBH;
            _callerForm = inCallerForm;
            cboTrangThaiTBBH.SelectedIndex = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            int TrangThaiTbiBH = (int)MyCommon.TrangThaiThietBiBH.DangOPhongKT;
            if(cboTrangThaiTBBH.SelectedIndex == 0)
            {
                TrangThaiTbiBH = (int)MyCommon.TrangThaiThietBiBH.ChoTra;
            }
            else if (cboTrangThaiTBBH.SelectedIndex == 1)
            {
                TrangThaiTbiBH = (int)MyCommon.TrangThaiThietBiBH.DaNhanVeChuaHoanThanh;
            } 
            else
            {
                TrangThaiTbiBH = (int)MyCommon.TrangThaiThietBiBH.DangOPhongKT;
            }
            //Thay doi trang thai
            Phien_ThietBiBH obj = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById(_phien_ThietBiBH);
            obj.TrangThaiThietBiBHID = TrangThaiTbiBH;
            Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(obj);

            //Luu lai lich su bao hanh
            LichSuBH LichSu = new LichSuBH();
            LichSu.LoaiLichSu = (int)MyCommon.LoaiLichSu.LichSuSuaChua;
            LichSu.Phien_ThietBiBHId = _phien_ThietBiBH;
            LichSu.ThoiGian = DateTime.Now;
            LichSu.NVThucHienId = frmMain.CurrentNhanVien.Id;
            string NoiDung = "Nhận về thiết bị BHSC từ NCC: " + cboNhaCungCap.SelectedItem.ToString();
            if(txtGhiChu.Text != "")
            {
                NoiDung+= "--- GhiChu: " + txtGhiChu.Text;
            }
            LichSu.NoiDung = NoiDung;
            LichSuBHBO.getInstance.InsertLichSuBH(LichSu);
            _callerForm.RefreshForm();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
